/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var ulcss="list_content",
licss="item",
imgcss="item_img",
headercss="list_content_header_search",
firsttitle="first_title",
secondtitle = "second_title",
hlicss="horizontal_item";
var topli = "top_item",infordiv="info",toptitle="top_title",topcnt="top_description",
topcntspe = "top_content_special",lycss="lyrics_item",
songtitle = "lyrics_song_title",lyricscontent = "lyrics_content";

function getXMLHttpRequest(){
    var xmlHttp =null;
    try{ // Firefox
        xmlHttp = new XMLHttpRequest();
    }catch(e){ // Internet Explorer 8
        try{
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function getReadyStateHandler(xmlHttpRequest){
    return function(){
        if(xmlHttpRequest.readyState==4){
            if(xmlHttpRequest.status==200){
                xmlHttpRequest.responseText;
            }else{
                alert("HTTP error "+xmlHttpRequest.status+": "+xmlHttpRequest.statusText);
            }
        }
    }; // end function
}

// DinhNP
function isFileExists(url) {
    if(url){
        var req = new XMLHttpRequest();
        req.open('GET', url, false);
        req.send();
        return req.status==200;
    } else {
        return false;
    }
}

// ThuanNH
// list songs: songs.push({
//                id:id,
//                isMV:isMV,
//                title:title,
//                views:views,
//                likeNumber:likeNumber,
//                artistList:artistList
//            });
function sortByViews(rs){
    for(i=0;i<rs.length;i++){
        for(j=1;j <=(rs.length-1);j++){

            var s1 = rs[j];
            var s2 = rs[j-1];

            if(parseInt(s2.views)<parseInt(s1.views)){
                // swap
                tmp = s1;
                rs[j] = s2;
                rs[j-1] = tmp;
            }// end swap
        }// end for j
    }// end for i
    
    return rs;
}

//TanND
function getURLParameters(paramName)
{
    var sURL = window.document.URL.toString();
    if (sURL.indexOf("?") > 0)
    {
        var arrParams = sURL.split("?");
        var arrURLParams = arrParams[1].split("&");
        var arrParamNames = new Array(arrURLParams.length);
        var arrParamValues = new Array(arrURLParams.length);
        var i = 0;
        for (i=0;i<arrURLParams.length;i++)
        {
            var sParam =  arrURLParams[i].split("=");
            arrParamNames[i] = sParam[0];
            if (sParam[1] != "")
                arrParamValues[i] = unescape(sParam[1]);
            else
                arrParamValues[i] = "No Value";
        }
        for (i=0;i<arrURLParams.length;i++)
        {
            if(arrParamNames[i] == paramName){
                //alert("Param:"+arrParamValues[i]);
                return arrParamValues[i];
            }
        }
        return false;
    }
}

//TanND
function loadXMLDoc(path){
    var xmlHttp = getXMLHttpRequest();
    xmlHttp.open("GET",path,false);
    xmlHttp.send();
    return xmlHttp.responseXML;
}

// ThuanNH
function removeWhitespace(str){
    str = str.replace(/ /g,"+");

    return str;
}

// ThuanNH
function restoreWhitespace(str){
    str = str.replace(/+/g," ");

    return str;
}

// ThuanNH
function SendXMLHttpRequestUrl(servlet,url){
    var xmlHttp=getXMLHttpRequest();

    xmlHttp.onreadystatechange=OnReadyStateChangeHandler(xmlHttp);
    
    xmlHttp.open("POST",servlet,true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(url);
}

// ThuanNH
function removeSeperator(str){
    str = str.replace(/\|/g, "\n");

    return str;
}

// ThuanNH
function displayArtistItem(item,context){
    var output ="";
    var imgsrc = context + "/db/" + item.name +"/"+ item.src;
    // output
    output += "<li class='"+licss+"'>"
    + "<div class='"+imgcss+"'>"
    + "<a href='artist.jsp?ArtistID="+item.id+"'>"
    + "<img src='"+imgsrc+"'/>"
    + "</a>"
    + "</div>"
    + "<p class='"+firsttitle+"'>"
    + "<a href='artist.jsp?ArtistID="+item.id+"'>"
    + item.name+"</a>"
    + "</p>"
    + "<p class='"+secondtitle+"'>"
    + item.views + " lượt nghe"
    + "</p>"
    + "</li>"

    return output;
}

// ThuanNH
function displayAlbumItem(item,context){
    var output="";
    var imgsrc = context + "/db/" + item.artistName +"/Album/"+item.title+"/"+item.src;
    // output
    output += "<li class='"+licss+"'>"
    + "<div class='"+imgcss+"'>"
    + "<a href='musicplayer.jsp?id="+item.id+"&type=2'>"
    + "<img src='"+imgsrc+"'/>"
    + "</a>"
    + "</div>"
    + "<div>"
    + "<p class='"+firsttitle+"'>"
    + "<a href='musicplayer.jsp?id="+item.id+"&type=2'>"
    + item.title+"</a>"
    + "</p>"
    + "<p class='"+secondtitle+"'>"
    + "<a href='artist.jsp?ArtistID="+item.artistId+"'>"
    + item.artistName+"</a>"
    + "</p>"
    + "<p class='"+secondtitle+"'>"
    + item.views + " lượt nghe"
    + "</p>"
    + "</div>"
    + "</li>"
    return output;
}

// ThuanNH
function displaySongItem(item,index){
    // output
    var output="";
    var src = getSongSRC(item);
    output += "<li class='"+licss+"'>"
    + "<div>"
    + "<p class='"+firsttitle+"'>"
    + "<a class='song_item' href='musicplayer.jsp?id="+item.id+"&type=1'>"
    + item.title+"</a>"
    + "</p>"
    + "<p class='"+secondtitle+"'>";
    var artistList = item.artistList;
    for(var j=0;j<artistList.length;j++){
        output+= "<a href='artist.jsp?artistid="+artistList[0].artist[j].artistId+"'>"
        + artistList[0].artist[j].artistName+"</a>"
        if(j!=artistList.length-1){
            output+=", ";
        }
    }
    output+= "</p>"
    + "<p class='"+secondtitle+"'>"
    + item.views + " lượt nghe"
    + "</p>"
    + "</div>"
    + "</li>";
    return output;
}

// ThuanNH
function displayLyricsItem(item){
    // output
    var output="";
    output += "<li class='"+lycss+"'>"
    + "<div class='"+songtitle+"'>"
    + "<a href='musicplayer.jsp?id="+item.id+"&type=1'>"
    + item.title
    + "</a>"
    + "</div>";
    output+="<div class='"+lyricscontent+"'>";
    var snt = item.sentences;
    for(var i=0;i<snt.length;i++){
        output+="<p>..."+snt[i]+"...</p>";
    }
    output+= "<div>"
    + "</li>";

    return output;
}

// ThuanNH
function seperateSentences(str){
    var sentences="";
    return sentences=str.split("|");
}

// ThuanNH
function emphasizedWord(des,start,word){
    var out = "";
    out += des.slice(0, start)+"<b>"+des.slice(start,start+word.length);
    out += "</b>"+des.slice(start+word.length,des.length);
    
    return out;
}

// ThuanNH
function findTopFitItem(rs){
    var idmax,max=0;

    if(rs.length>0){
        for(var i =0;i<rs.length;i++){
            if(rs[i].per>max){
                idmax=i;
                max = rs[i].per;
            }
        }// end for

        return idmax;
    }else{
        return -1;
    }
}

// ThuanNH
function findMax(art,alb,son){
    max = Math.max(art, alb,son);

    if(max==0){
        return 0;
    }
    if(max==art){
        return 1;
    }
    if(max==alb){
        return 2;
    }
    if(max==son){
        return 3;
    }
}

// ThuanNH
function displayNoneItem(query){
    // output
    var output="<p><h1>Không có kết quả phù hợp cho: <i>"+query+"</i><br/>Vui lòng thử lại...</h1></p>";

    return output;
}

// ThuanNH
String.prototype.removeDotCharacter = function(){
    var str = this.toString();
    for(var i = -1;Math.abs(i)<this.toString().length+1;i--){
        if((this.slice(i))=="."){
            str = this.slice(0,i);
        }else{
            break;
        }
    } // end for
    return str;
}

// ThuanNH
function displayMVItem(item){
    // output
    var output="";
    var src = getMVSRC(item);
    var capture = getCapture(item);

    output += "<li class='"+licss+"'>";
    output += "<div class='"+imgcss+"'>";
    output += "<a href='musicplayer.jsp?id="+item.id+"&type=4'>";
    output += "<img src='"+capture+"'/>"
    output += "</a>"
    output += "</div>"
    output += "<p class='"+firsttitle+"'>"
    + "<a href='musicplayer.jsp?id="+item.id+"&type=4'>";
    output += item.title+"</a>"
    output += "</p>";
    output += "<p class='"+secondtitle+"'>";
    var artistList = item.artistList;
    for(var j=0;j<artistList.length;j++){
        output+= "<a href='artist.jsp?artistid="+artistList[0].artist[j].artistId+"'>"
        + artistList[0].artist[j].artistName+"</a>"
        if(j!=artistList.length-1){
            output+=", ";
        }
    }
    output+= "</p>"
    + "</li>";
    return output;
}

// ThuanNH
function displayTopAsArtistItem(obj,context){
    var output ="";
    var imgsrc = context + "/db/" + obj.name.removeDotCharacter() +"/"+ obj.src;
    // output
    output += "<li class='"+topli+"'>"
    + "<a href='artist.jsp?ArtistID="+obj.id+"'>"
    + "<img src='"+imgsrc+"'/>"
    + "</a>"
    + "<div class='info'>"
    + "<p class='"+toptitle+"'>"
    + "<a href='artist.jsp?ArtistID="+obj.id+"'>"
    + obj.name+"</a>"
    + "</p>"
    + "<p class='"+topcnt+"'>"
    + obj.biology.wrap()
    + "</p>"
    + "<p class='"+topcnt+"'>"
    + obj.views + " lượt nghe"
    + "</p>"
    + "</div>"
    + "</li>"

    return output;
}

// ThuanNH
String.prototype.wrap = function(){
    if(this.length>100){
        return this.substring(0, 200) + "...";
    }
    return this;
}

// ThuanNH
function displayTopAsAlbumItem(obj,context){
    var output ="";
    var imgsrc = context + "/db/" + obj.artistName +"/Album/"+obj.title+"/"+obj.src;
    // output
    output += "<li class='"+topli+"'>"
    + "<a href='musicplayer.jsp?id="+obj.id+"&type=2'>"
    + "<img src='"+imgsrc+"'/>"
    + "</a>"
    + "<div class='info'>"
    + "<p class='"+toptitle+"'>"
    + "<a href='musicplayer.jsp?id="+obj.id+"'&type=2>"
    + obj.title+"</a>"
    + "</p>"
    + "<p class='"+topcntspe+"'>"
    + "<a href='artist.jsp?ArtistID="+obj.id+"'>"
    + obj.artistName+"</a>"
    + "</p>"
    + "<p class='"+topcnt+"'>"
    + obj.desc.wrap()
    + "</p>"
    + "<p class='"+topcnt+"'>"
    + obj.views + " lượt nghe"
    + "</p>"
    + "</div>"
    output += "</li>"

    return output;
}

// ThuanNH
function displayTopAsSongItem(obj,context){
    var output ="";
    var src = context+"/db/"+obj.artistList[0].artist[0].artistName+"/MV/"+obj.src.trim();
    if(obj.isMV=="false"){
        // output
        output += "<li class='"+topli+"'>"
        + "<div class='song_info'>"
        + "<p class='"+toptitle+"'>"
        + "<a href='musicplayer.jsp?id="+obj.id+"&type=1'>"
        + obj.title+"</a>"
        + "</p>"
        + "<p class='"+topcntspe+"'>";
        for(var i=0;i<obj.artistList.length;i++){
            output+="<a href='artist.jsp?ArtistID="+obj.id+"'>";
            output+= obj.artistList[0].artist[i].artistName;
            output+="</a>";
            if(i!=obj.artistList.length-1){
                output+=", ";
            }
        }
        output+= "</p>";
        output+= "<p class='"+topcnt+"'>"
        + obj.views + " lượt nghe"
        + "</p>"
        + "</div>";
        output += "</li>"
    }else{
        output += "<li class='"+topli+"'>"
        + "<a href='musicplayer.jsp?id="+obj.id+"&type=2'>"
        + "<img src='"+src+".jpg"+"'/>"
        + "</a>"
        + "<div class='info'>"
        + "<p class='"+toptitle+"'>"
        + "<a href='musicplayer.jsp?id="+obj.id+"'&type=2>"
        + obj.title+"</a>"
        + "</p>"
        + "<p class='"+topcntspe+"'>";
        for(var i=0;i<obj.artistList.length;i++){
            output+="<a href='artist.jsp?ArtistID="+obj.id+"'>";
            output+= obj.artistList[0].artist[i].artistName;
            output+="</a>";
            if(i!=obj.artistList.length-1){
                output+=", ";
            }
        }
        output+= "</p>"
        + "<p class='"+topcnt+"'>"
        + obj.views + " lượt nghe"
        + "</p>"
        + "</div>";
        output += "</li>"
    }
    
   
    return output;
}

// ThuanNH
function displayTopAsLyricsItem(obj){
    // output
    var output="";
    output += "<li class='"+lycss+"'>"
    + "<div class='"+songtitle+"'>"
    + "<a href='musicplayer.jsp?id="+item.id+"&type=1'>"
    + item.title
    + "</a>"
    + "</div class='"+lyricscontent+"'>";
    output+="<div>";
    var snt = item.sentences;
    for(var i=0;i<snt.length;i++){
        output+="<p>..."+snt[i]+"...</p>";
    }
    output+= "<div>"
    + "</li>";

    return output;
}

// ThuanNH
function getXMLFromStorage(filename){
    var xml = localStorage.getItem(filename);
    if(xml!=null){
        return new DOMParser().parseFromString(xml,"text/xml");
    }
    return false;
}

// ThuanNH
function pushXMLToStorage(filename,context){
    if(localStorage.getItem(fileName)==null){
        var dom = loadXMLDoc(context+filename);
        var xml = new XMLSerializer().serializeToString(dom);
        localStorage.setItem(xml);
        return true;
    }
    return false;
}

// ThuanNH
function getWeekNo() {
    var totalDays = 0;
    now = new Date();
    years=now.getYear()
    if (years < 1000)
        years+=1900
    var days = new Array(12); // Array to hold the total days in a month
    days[0] = 31;
    days[2] = 31;
    days[3] = 30;
    days[4] = 31;
    days[5] = 30;
    days[6] = 31;
    days[7] = 31;
    days[8] = 30;
    days[9] = 31;
    days[10] = 30;
    days[11] = 31;

    //  Check to see if this is a leap year

    if (Math.round(now.getYear()/4) == now.getYear()/4) {
        days[1] = 29
    }else{
        days[1] = 28
    }

    //  If this is January no need for any fancy calculation otherwise figure out the
    //  total number of days to date and then determine what week

    if (now.getMonth() == 0) {
        totalDays = totalDays + now.getDate();
    }else{
        var curMonth = now.getMonth();
        for (var count = 1; count <= curMonth; count++) {
            totalDays = totalDays + days[count - 1];
        }
        totalDays = totalDays + now.getDate();
    }
    var week = Math.round(totalDays/7);
    return week;
}

